﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tetris
{
    abstract class Figure : IRenderable, ICollidable, IProducer
    {
        protected MatrixCoords topLeft;
        protected char[,] body;

        public ConsoleColor Color { get; protected set; }
        public bool IsDestroyed { get; set; }

        protected Figure(MatrixCoords topLeft, char[,] body, ConsoleColor color)
        {
            this.topLeft = topLeft;
            this.body = body;
            this.Color = color;
            this.IsDestroyed = false;
        }

        public virtual void Update()//leave collision dispatcher to check for collisions
        {
            Move(Direction.Down);
        }

        public virtual void RespondToCollision()
        {
            this.IsDestroyed = true;
        }
		
        public virtual void Rotate()
        {
            char[,] rotated = new char[this.body.GetLength(1), this.body.GetLength(0)];

            int rowLength = this.body.GetLength(0);
            int colLength = this.body.GetLength(1);

            for (int c = 0; c < colLength; c++)
            {
                for (int r = rowLength - 1; r >= 0; r--)
                {
                    rotated[c, rowLength - 1 - r] = this.body[r, c];
                }
            }

            this.body = rotated;
        }

        public void Move(Direction d)
        {
            switch (d)
            {
                case Direction.Left:
                    {
                        this.topLeft += new MatrixCoords(0, -1);
                        break;
                    }
                case Direction.Right:
                    {
                        this.topLeft += new MatrixCoords(0, 1);
                        break;
                    }
                case Direction.Down:
                    {
                        this.topLeft += new MatrixCoords(1, 0);
                        break;
                    }
                case Direction.Up:
                    {
                        this.topLeft += new MatrixCoords(-1, 0);
                        break;
                    }
                default:
                    break;
            }
        }

        public MatrixCoords GetTopLeft()
        {
            return this.topLeft;
        }

        public MatrixCoords GetTopRight()
        {
            return new MatrixCoords(this.topLeft.Row, this.topLeft.Col + body.GetLength(1) - 1);
        }

        public char[,] GetImage()
        {
            return this.body.Clone() as char[,];
        }

        public ConsoleColor GetColor()
        {
            return this.Color;
        }

        public virtual IEnumerable<Figure> ProduceFigures()
        {
            List<Figure> producedFigures = new List<Figure>();
            if (this.IsDestroyed)
            {
                int rowLength = this.body.GetLength(0);
                int colLength = this.body.GetLength(1);

                for (int r = 0; r < rowLength; r++)
                {
                    for (int c = 0; c < colLength; c++)
                    {
                        if (body[r, c] != ' ')
                        {
                            producedFigures.Add(new Block(new MatrixCoords(this.GetTopLeft().Row + r, this.GetTopLeft().Col + c), this.GetColor()));
                        }
                    }
                }
            }

            return producedFigures;
        }
    }
}
